package com.idas.comp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.idas.comp.enums.ResourceType;
import com.idas.db.DBFilter;
import com.idas.wsdl.XmlDocumentation;

public class Resource {
	@XmlDocumentation(value = "The unique UUID of the resource")
	private String uuid;
	@XmlDocumentation(value = "The name of the resource")
	private String name;
	@XmlDocumentation(value = "The type of the resource")
	private ResourceType type;

	
	static {
		DBFilter.registerClass(Resource.class);
	}
	
	public Resource() {
	}

	protected Resource(ResultSet rs) throws SQLException {
		uuid = rs.getString("system_uuid");
		name = rs.getString("name");
		type = ResourceType.getValue(rs.getInt("resource_id"));
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void setName(Connection conn, String name)throws SQLException{
		String sql = "UPDATE system_uuid SET name = ? WHERE uuid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, name);
		ps.setString(2, getUuid());
		ps.execute();
		ps.close();
		setName(name);
	}
	
	public ResourceType getType() {
		return type;
	}

	public void setType(ResourceType type) {
		this.type = type;
	}
	
	public void delete(Connection conn)throws Exception{
		String table = "";
		String sql = "";
		PreparedStatement ps;
		ResultSet rs;
		
		int userCount = 0;
		int moduleCount = 0;
		
		switch(this.getType()){
		case EXPENSE:
			table = "expense";
			break;
		case MODULE:
			sql = "SELECT COUNT(um.*) FROM user_module um, module m WHERE m.module_id = um.module_id AND m.uuid = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, getUuid());
			rs = ps.executeQuery();
			while(rs.next()){
				userCount = rs.getInt(1);
			}
			rs.close();
			ps.close();
			
			sql = "SELECT COUNT(*) FROM expense WHERE module_uuid = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, getUuid());
			rs = ps.executeQuery();
			while(rs.next()){
				moduleCount = rs.getInt(1);
			}
			rs.close();
			ps.close();
			
			table = "module";
			break;
		case USER:
			
			sql = "SELECT COUNT(um.*) FROM user_module um, user u WHERE u.user_id = um.user_id AND u.uuid = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, getUuid());
			rs = ps.executeQuery();
			while(rs.next()){
				moduleCount = rs.getInt(1);
			}
			rs.close();
			ps.close();
			
			sql = "SELECT COUNT(*) FROM expense WHERE user_uuid = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, getUuid());
			rs = ps.executeQuery();
			while(rs.next()){
				moduleCount = rs.getInt(1);
			}
			rs.close();
			ps.close();
			
			table = "user";
		}
		
		if(userCount>0){
			throw new Exception("Users are connected to this resource");
		}
		
		if(moduleCount>0){
			throw new Exception("Modules are connected to this resource");
		}
		
		if (table.length() > 0){
			sql = "DELETE FROM "+table+" WHERE uuid = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, this.getUuid());
			ps.execute();
			ps.close();
		}
		
		sql = "DELETE FROM system_uuid WHERE system_uuid = ?";
		ps = conn.prepareStatement(sql);
		ps.setString(1, this.getUuid());
		ps.execute();
		ps.close();
		
	}

	public static Resource getResourceForUUID(Connection conn, String uuid) throws SQLException{
		
		Resource res = null;
		
		String sql = "SELECT * FROM system_uuid WHERE system_uuid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, uuid);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			res = new Resource(rs);
		}
		rs.close();
		ps.close();
		
		return res;
	}
	
	public static Resource addResourceToDatabase(Connection conn, String uuid, String name, long userId, ResourceType type)throws SQLException{
		
		String sql = "INSERT INTO system_uuid (system_uuid, user_id, name, resource_id) VALUES (?,?,?,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, uuid);
		ps.setLong(2, userId);
		ps.setString(3, name);
		ps.setInt(4, ResourceType.getValue(type));
		
		ps.execute();
		ps.close();
		return getResourceForUUID(conn, uuid);
	}

}
